package warthog.parsegen;


class Dot 
//implements Comparable<Dot> 
{
	final Rule rule;
	final int offset;
	public Dot(Rule r, int off) {
		rule = r;
		offset = off;
	}

	@Override
	public int hashCode() {
		return rule.hashCode()+offset;
	}
	@Override
	public boolean equals(Object obj) {
		if (this==obj) return true;
		if (obj instanceof Dot) {
			Dot d = (Dot) obj;
			return rule==d.rule && offset==d.offset;
		}
		return false;
	}
	public Sym head() {
		return offset < rule.body.length ? rule.body[offset] : null;
	}
	public Dot tail() {
		return offset < rule.body.length ? rule.dot(offset+1) : null;
	}
	public String toString() {
		StringBuilder sb = new StringBuilder(rule.head.name+"->");
		for (int i=0; i<rule.body.length; i++) {
			sb.append((i==offset)?'.':' ');
			sb.append(rule.body[i].name);
		}
		sb.append((rule.body.length==offset)?'.':' ');
		return sb.toString();
	}


	public boolean atEnd() {
		return offset >= rule.body.length;
	}
//	@Override
//	public int compareTo(Dot o) {
//		if (rule == o.rule) return offset-o.offset; 
//		else return rule.id - o.rule.id;
//	}
}